﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using dotNetMembership = System.Web.Security.Membership;
using dotNetRoles = System.Web.Security.Roles;
using Binding = System.Web.Mvc.BindingHelperExtensions;
using StarterKits.Mvc.Filters.FormsAuthentication;
using StarterKits.Mvc.Membership.Controllers;
using P.InOut.NetMvc.Logging;
using P.InOut.NetMvc.Properties;
using P.InOut.NetMvc.Common;
using P.InOut.NetMvc.Models;

namespace P.InOut.NetMvc.Controllers
{
    public class UpdatesController : InOutController
    {
        public UpdatesController()
        {
            ViewData["ErrorMessage"] = string.Empty;
        }

        [RequiresAuthentication()]
        public override ActionResult Index()
        {
            using (InOutDataContext db = new InOutDataContext(Settings.Default.dbConn))
            {
                return View("Index");
            }
        }

        [RequiresAuthentication()]
        public override ActionResult Detail(object id)
        {
            return View("Detail");
        }

        [NonAction]
        public virtual void OnBeforeCreate() { }

        [NonAction]
        public virtual void OnAfterCreate() { }

        [RequiresAuthentication()]
        public override ActionResult Create()
        {
            using (InOutDataContext db =
                new InOutDataContext(Settings.Default.dbConn))
            {
                // Get current user
                MembershipUser user = Membership.GetUser();

                OnBeforeCreate();

                // Create new Update and set Properties
                Update update = new Update
                {
                    Active          = true,
                    UserId          = (Guid)user.ProviderUserKey,
                    DateTimeEntered = DateTime.Now,
                    UpdateText      = Request.Form["UpdateText"]
                };

                // Insert and Submit Changes to the db
                db.Updates.InsertOnSubmit(update);
                db.SubmitChanges();

                // Create new UpdateActivity and set properties
                UpdateActivity activity = new UpdateActivity
                {
                    Activity            = "Insert",
                    DateTimeActivity    = DateTime.Now.Ticks,
                    UpdateId            = update.Id,
                    UserId              = (Guid)user.ProviderUserKey
                };

                // Insert and Submit Changes to the db
                db.UpdateActivities.InsertOnSubmit(activity);
                db.SubmitChanges();

                OnAfterCreate();

                return RedirectToAction("Index", "Updates");
            }
        }

        [NonAction]
        public virtual void OnBeforeUpdate(object id) { }

        [NonAction]
        public virtual void OnAfterUpdate(object id) { }

        [RequiresAuthentication]
        public override ActionResult Update(object id)
        {
            using (InOutDataContext db =
                new InOutDataContext(Settings.Default.dbConn))
            {
                Update update = db.Updates.Single<Update>(u => u.Id == int.Parse(id.ToString()));

                MembershipUser user = dotNetMembership.GetUser();
                if ((Guid)user.ProviderUserKey == update.UserId)
                {
                    update.UpdateText = Request.Form["UpdateText"];

                    OnBeforeUpdate(id);

                    // Create new UpdateActivity and set properties
                    UpdateActivity activity = new UpdateActivity
                    {
                        Activity            = "Update",
                        DateTimeActivity    = DateTime.Now.Ticks,
                        UpdateId            = update.Id,
                        UserId              = (Guid)user.ProviderUserKey
                    };

                    // Insert and Submit Changes to the db
                    db.UpdateActivities.InsertOnSubmit(activity);
                    db.SubmitChanges();



                    OnAfterUpdate(id);
                }

                return RedirectToAction("Index", "Updates");
            }
        }

        [NonAction]
        public virtual void OnBeforeDelete() { }

        [NonAction]
        public virtual void OnAfterDelete() { }

        [RequiresAuthentication]
        public override ActionResult Delete(object id)
        {
            using (InOutDataContext db =
                new InOutDataContext(Settings.Default.dbConn))
            {
                Update update = db.Updates.Single<Update>(u => u.Id == int.Parse(id.ToString()));

                MembershipUser user = dotNetMembership.GetUser();
                if ((Guid)user.ProviderUserKey == update.UserId)
                {
                    // Delete update
                    update.Active = false;

                    // Create new UpdateActivity and set properties
                    UpdateActivity activity = new UpdateActivity
                    {
                        Activity            = "Delete",
                        DateTimeActivity    = DateTime.Now.Ticks,
                        UpdateId            = update.Id,
                        UserId              = (Guid)user.ProviderUserKey
                    };

                    // Insert and Submit Changes to the db
                    db.UpdateActivities.InsertOnSubmit(activity);
                    db.SubmitChanges();
                }

                return RedirectToAction("Index", "Updates");
            }
        }
    }
}
